package com.jd.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.jd.entity.Config;
import com.jd.entity.RealnameEntity;
import com.jd.entity.UserEntity;
import com.jd.service.MatchService;
import com.jd.service.UserService;
/**
 * @author masicheng 
 * 时间 : 2020年12月18日 下午2:06:47 
 * 注释 :yonghu
 */

@Controller
@RequestMapping("/userController")
public class UserController {
	
	@Autowired
	private MatchService matchService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/getUserRole")
	@ResponseBody
	public HashMap<String, Object> getUserRole(HttpServletRequest request) {
		String userId =(String)request.getSession().getAttribute("userId");
		String wxName =(String)request.getSession().getAttribute("nickname");
		String id = request.getParameter("id");//比赛id
		HashMap<String, Object> matchContent = matchService.getMatchContentCode(id);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("lowRank", matchContent.get("lowest_rank"));
		map.put("heightRank", matchContent.get("highest_rank"));
		map.put("region", matchContent.get("region"));//大区
		map.put("gameName", matchContent.get("gameName"));//游戏
		HashMap<String, Object> userRole = userService.getUserRole(map);
		if(userRole!=null){
			userRole.put("wxName", wxName);
			return userRole;
		}else{
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("wxName", wxName);
			return hashMap;
		}
	}
//实名认证
	@RequestMapping("/inRealName")
	@ResponseBody
	public Boolean inRealName(HttpServletRequest request) {
		String userId = request.getSession().getAttribute("userId").toString();
		String realname = request.getParameter("realname");
		String idcard = request.getParameter("idcard");
		String id = request.getParameter("id");
		RealnameEntity realnameEntity = new RealnameEntity();
		realnameEntity.setId(id);
		realnameEntity.setRealName(realname);
		realnameEntity.setIdCard(idcard);
		realnameEntity.setDelflag("0");
		realnameEntity.setState("1");
		realnameEntity.setUserId(userId);
		realnameEntity.setStartTime(new Date());
		realnameEntity.setUpdateBy(userId);
		realnameEntity.setUpdateTime(new Date());
		boolean i = userService.inRealName(realnameEntity);
		return i;
	}
	//实名认证返显
	@RequestMapping("/getRealName")
	@ResponseBody
	public RealnameEntity getRealName(HttpServletRequest request) {
		String userId = request.getSession().getAttribute("userId").toString();
		RealnameEntity realnameEntity = userService.getRealName(userId);
		return realnameEntity;
	}
	
	//我的
	@RequestMapping("/getUser")
	@ResponseBody
	public HashMap<String,Object> getUser(HttpServletRequest request,HttpServletResponse response) {
		String userId = (String)request.getSession().getAttribute("userId");
		HashMap<String,Object> map = userService.getUser(userId);
		return map;
	}
	@RequestMapping("/getUserByOpenId")
	@ResponseBody
	public String getUserByOpenId(HttpServletRequest request,HttpServletResponse response) {
		String openId = (String)request.getSession().getAttribute("openId");
		if(openId.equals("null") || openId==null || openId.equals(null)){
			try {
				response.sendRedirect("http://www.bfwcg.com/game/wechat/authorize?returnUrl=asdasdasd");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return openId;
	}
	//我的
	@RequestMapping("/getUser1")
	public void getUser1(HttpServletRequest request,HttpServletResponse response) {
		String userId = (String)request.getSession().getAttribute("userId");
		if(userId==null || userId==""){
			try {
				response.sendRedirect("http://www.bfwcg.com/game/wechat/authorize?returnUrl=asdasdasd");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//我的推广员
		@RequestMapping("/getOnUserList")
		@ResponseBody
		public ArrayList<HashMap<String, Object>> getOnUserList(HttpServletRequest request) {
			String userId = request.getSession().getAttribute("userId").toString();
			ArrayList<HashMap<String, Object>> onUserList = userService.getOnUserList(userId);
			return onUserList;
		}
		//申请推广员
		@RequestMapping("/extension")
		@ResponseBody
		public Boolean extension(HttpServletRequest request) {
			String userId = request.getSession().getAttribute("userId").toString();
			String realname = request.getParameter("realname");
			String idcard = request.getParameter("idcard");
			String id = request.getParameter("id");
			String realnameType = request.getParameter("realnameType");
			if(realnameType.equals("0")){
				RealnameEntity realnameEntity = new RealnameEntity();
				realnameEntity.setId(id);
				realnameEntity.setRealName(realname);
				realnameEntity.setIdCard(idcard);
				realnameEntity.setDelflag("0");
				realnameEntity.setState("1");
				realnameEntity.setUserId(userId);
				realnameEntity.setStartTime(new Date());
				realnameEntity.setUpdateBy(userId);
				realnameEntity.setUpdateTime(new Date());
				userService.inRealName(realnameEntity);
			}
			UserEntity user = new UserEntity();
			user.setId(userId);
			user.setType("0");
			user.setApplyTime(new Date());
			Boolean i=userService.extension(user);
			return i;
		}
	

/*@RequestMapping("/wxshare")
	@ResponseBody
	public String wxshare(HttpServletRequest request, String url) {
		Config config = new Config();
		config.setNonceStr(WXUtils.getNonceStr());
		// 时间戳
		config.setTimestamp(String.valueOf(System.currentTimeMillis()/1000));
 
		config.setUrl(url);
		config.setAppid("wx368eaef1845f00c6");
 
		String signature = WxUtils.getSignature(config);
		config.setSignature(signature);
		return new Gson().toJson(config);
	}*/
}
